package com.ylfstu.homework;

import java.util.Scanner;

public class Homework02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("===请输入信息：===");

        while (true) {
            System.out.print("用户名：");
            try {
                String username = scanner.next();
                if (!(username.length() >= 2 && username.length() <= 4)) {
                    throw new ArrayIndexOutOfBoundsException("输入用户名长度要在2~4之间");
                }
                break;
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println(e.getMessage());
            }
        }

        while (true) {
            System.out.print("密码：");
            try {
                String psd = scanner.next();
                int i = Integer.parseInt(psd);
                if (psd.length() != 6) {
                    throw new ArrayIndexOutOfBoundsException("密码长度应为6");
                }
                break;
            } catch (NumberFormatException e) {
                System.out.println("密码必须全为数字");
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println(e.getMessage());
            }
        }

        while (true) {
            System.out.print("邮箱：");
            try {
                String email = scanner.next();
                int i = email.indexOf("@");
                int i1 = email.indexOf(".");
                if (i == -1 || i1 == -1) {
                    throw new ArrayIndexOutOfBoundsException("邮箱中必须包含@和.");
                }
                if (!(i < i1)) {
                    throw new ArrayIndexOutOfBoundsException("@需要在.前面");
                }
                break;
            } catch (RuntimeException e) {
                System.out.println(e.getMessage());
            }
        }
        System.out.println("邮箱注册成功！");
    }

}
